package com.baosight.sczx.common.zm.cInterface.cg;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.dao.Dao;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cn.CoalYield;
import com.baosight.sczx.zm.cg.domain.ZMCGDispatchDaily;
import com.baosight.sczx.zm.cg.domain.ZMCGGrubReportFinal;
import com.baosight.sczx.zm.cg.domain.ZMCGMineReportFinal;
import com.baosight.sczx.zm.ch.domain.ZMCHProduceReport;
import com.baosight.sczx.zm.cs.domain.ZMCSWorkSchedule;

/**
 * 调度生产日报生成
 * <p>Title: DailyReport.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 王伟
 * @version 1.0
 * <pre>Histroy:
 *          2016-9-1 15:14:16 王伟  Create
 *</pre>
 */

public class DailyReport {
	private static Dao dao = (Dao)SpringApplicationContext.getBean("dao");
	
    /**
     * <p>Description: </p> 得到调度生产日志的对象属性
     * @param fdVirtualDate 调度日期
     * @param fdMiningId   矿井id
     * @param fdMiningName 矿井名称
     * @return
     * <p>History:
	 *    2016-9-1 15:15:29 王伟
	 * </p>     
	 */
	@SuppressWarnings("rawtypes")
	private static HashMap getDispatchDaily(String fdVirtualDate,String fdMiningId){
		HashMap<Object, Object> params = new HashMap<Object, Object>();
		params.put("fdVirtualDate", fdVirtualDate);
		params.put("fdMiningId", fdMiningId);
		List list = dao.query("ZMCGDispatchDaily.query",params);
		String dispatchDailyId = "";//矿区调度日志主表的id
		if(list!= null && list.size()>0){
			ZMCGDispatchDaily bean = (ZMCGDispatchDaily)list.get(0);
			params.put("fdKeyId", bean.getFdKeyId());
			params.put("fdMinesumDay", bean.getFdMinesumDay());//早班产量（吨）
			params.put("fdMinesumMiddle", bean.getFdMinesumMiddle());//中班产量（吨）
			params.put("fdMineSumUpMid", bean.getFdMineSumUpMid());
			params.put("fdMineSumDownMid", bean.getFdMineSumDownMid());
			params.put("fdMinesumMiddle", bean.getFdMinesumMiddle());
			params.put("fdMinesumNight", bean.getFdMinesumNight());//晚班产量（吨）
			params.put("fdMinesumDsum", bean.getFdMinesumDsum());//产量日累计(吨)
			params.put("fdMinesumMsum", bean.getFdMinesumMsum());//产量月累计(吨)
			params.put("fdMinesumYsum", bean.getFdMinesumYsum());//产量年累计(吨)
			params.put("fdMovelenghDay", bean.getFdMovelenghDay());//早班进尺（米）
			params.put("fdMovelenghMiddle", bean.getFdMovelenghMiddle());//中班进尺（米）
			params.put("fdMoveLenghUpMid", bean.getFdMoveLenghUpMid());
			params.put("fdMoveLenghDownMid", bean.getFdMoveLenghDownMid());
			params.put("fdMovelenghNight", bean.getFdMovelenghNight());//晚班进尺（米）
			params.put("fdMovelenghDsum", bean.getFdMovelenghDsum());//进尺日累计（米）
			params.put("fdMovelenghMsum", bean.getFdMovelenghMsum());//进尺月累计（米）
			params.put("fdMovelenghYsum", bean.getFdMovelenghYsum());//进尺年累计（米）
			params.put("fdVirtualDate", bean.getFdVirtualDate());//日期
			params.put("fdMiningId", bean.getFdMiningId());
			params.put("fdMiningName",bean.getFdMiningName());
			params.put("fdMineMzSumDsum", bean.getFdMineMzSumDsum());//mz产量日累计(吨)
//			params.put("fdMoveChSumDsum", bean.getFdMoveChSumDsum());//旬报掘进日累计
			
		}else{
			 params.clear();
			 dispatchDailyId = TablePk.getInstance().getNextValue("T_CG_DISPATCHDAILY");
			 params.put("fdKeyId", dispatchDailyId);
			 params.put("fdVirtualDate", fdVirtualDate);
			 params.put("fdMiningId", fdMiningId);
			 //第一次上报数据 -- 煤质采煤量
			 BigDecimal fdMineMzSumDsum = CoalYield.getMinesum(1, fdVirtualDate, fdMiningId, "", "");
			 params.put("fdMineMzSumDsum", fdMineMzSumDsum);
			 //掘进进尺量
	         dao.insert("ZMCGDispatchDaily.insert", params);
		}
        return params;    	
    }
	/**
	 * <p>Function: getWorkScheduleName</p>
	 * <p>Description: </p>
	 * 得到班次名称
	 * fdWorknumId:班次ID
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-9-1 15:15:29 王伟
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private static String getWorkScheduleName(String fdWorknumId){
		String banci = "";//班次名称
		HashMap<Object, Object> params = new HashMap<Object, Object>();
		params.put("fdKeyId", fdWorknumId);
		List list = dao.query("ZMCSWorkSchedule.query",params);
		if(list!=null&&list.size()>0){
			ZMCSWorkSchedule _tmpbean = (ZMCSWorkSchedule)list.get(0);
			banci = _tmpbean.getFdScheduleNum();
		}
		return banci;
    }
	
    /**
     * 得到班采矿量
     * @param fdWorknumId
     * @param fdVirtualDate
     * @param fdMiningId
     * @return
     */
	@SuppressWarnings("rawtypes")
	private static BigDecimal getMineSumByWorknum(String fdWorknumId,String fdVirtualDate,String fdMiningId){
		HashMap<Object, Object> params = new HashMap<Object, Object>();
		params.put("fdWorknumId", fdWorknumId);
		params.put("fdVirtualDate", fdVirtualDate);
		params.put("fdMiningId", fdMiningId);
		List list = dao.query("ZMCGMineReportFinal.querySum",params);
		BigDecimal banCaikuang = null; //班采矿量
        if(list!=null&&list.size()>0){
        	ZMCGMineReportFinal _tmpbean = (ZMCGMineReportFinal)list.get(0);
        	banCaikuang = _tmpbean.getFdMineSum();
        }              	
        return banCaikuang;
	}
	
    /**
     * 得到采矿量日合计、月合计、年合计 -- 调度日报
     * @param fdVirtualDate 调度日期
     * @param fdMiningId 矿井id
     * @param type 类型：1 表示 日合计；2表示月合计；3表示年合计；4表示从煤质读取数据。
     * @return
     */
	@SuppressWarnings("unused")
	private static BigDecimal getMineSum(String fdVirtualDate,String fdMiningId,int type){
		HashMap<Object, Object> params = new HashMap<Object, Object>();
		BigDecimal mineSum = null;
		String year = StaticTools.getYearStr(fdVirtualDate);
		String month = StaticTools.getMonthStr(fdVirtualDate); 
		if(month.length()<2)
			month = "0"+month;
		String fdBeginDate_month = year+"-"+month+"-01";
		String fdBeginDate_year = year+"-01-01";
		
		if(type == 1)
			mineSum = getMineSumDayReport(1, fdVirtualDate, fdMiningId, "", "");
		else if(type == 2)
			mineSum = getMineSumDayReport(2, "", fdMiningId, fdBeginDate_month, fdVirtualDate);
		else if(type == 3)
			mineSum = getMineSumDayReport(3, "", fdMiningId, fdBeginDate_year, fdVirtualDate);
		//煤质日数据
		else if(type == 4)
			mineSum = CoalYield.getMinesum(1, fdVirtualDate, fdMiningId, "", "");
        return mineSum;
	}
	/**
	 * 调度日报煤炭产量上报总值
	 * @param type 类型：1表示日累计；2表示月累计；3表示年累计
	 */
	@SuppressWarnings("rawtypes")
	public static BigDecimal getMineSumDayReport(int type,String fdVirtualDate,String fdMiningId,String beginDate,String endDate){
		BigDecimal minesum = null;
		HashMap<Object, Object> params = new HashMap<Object, Object>();
		if(1==type)
			params.put("fdVirtualDate", fdVirtualDate);
		else{
			params.put("fdbeginDate", beginDate);
			params.put("fdEndDate", endDate);
		}
		params.put("fdMiningId", fdMiningId);
		List list = dao.query("ZMCGMineReportFinal.querySum",params);
        if(list!=null&&list.size()>0){
        	ZMCGMineReportFinal _tmpbean = (ZMCGMineReportFinal)list.get(0);
        	minesum = _tmpbean.getFdMineSum();
        }              	
 		return minesum;
	}

	/**
	 * 
	 * <p>Function: generateMine</p>
	 * <p>Description: </p>
	 * 生成调度日报中的采矿数据
	 * fdWorknumId:班次ID
	 * fdVirtualDate：调度日期
	 * fdMiningId：矿井ID
	 * fdMiningName：矿井名称
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-9-1 15:15:29 王伟
	 * </p>
	 *
	 */
	@SuppressWarnings("unchecked")
	public static String generateMine(String fdWorknumId,String fdVirtualDate,String fdMiningId,String fdMiningName){
		HashMap<Object, Object> params = new HashMap<Object, Object>();
		
		params = getDispatchDaily(fdVirtualDate,fdMiningId);//矿区调度日志主表的id
		String banci = getWorkScheduleName(fdWorknumId);//班次名称
		BigDecimal banCaikuang = getMineSumByWorknum(fdWorknumId,fdVirtualDate,fdMiningId); //班采矿量
		BigDecimal riCaikuang = getMineSum(fdVirtualDate,fdMiningId,1); //日采矿量
		//煤质采煤更新数据
		BigDecimal fdMineMzSumDsum =getMineSum(fdVirtualDate,fdMiningId,4);
		
		BigDecimal yueCaikuang = getMineSum(fdVirtualDate,fdMiningId,2); //月采矿量
		BigDecimal nianCaikuang = getMineSum(fdVirtualDate,fdMiningId,3); //年采矿量

        //更新矿区调度日志表的数据
		params.put("fdKeyId",params.get("fdKeyId"));
		params.put("fdVirtualDate", fdVirtualDate);
		params.put("fdMiningId", fdMiningId);
		params.put("fdMiningName",fdMiningName);
		params.put("fdMinesumDsum",riCaikuang);
		params.put("fdMinesumMsum",yueCaikuang);
		params.put("fdMinesumYsum",nianCaikuang);
		params.put("fdMineMzSumDsum",fdMineMzSumDsum);
		if("早班".equals(banci.trim()))
			params.put("fdMinesumDay",banCaikuang);
		else if("中班".equals(banci.trim()))
			params.put("fdMinesumMiddle",banCaikuang);
		else if("晚班".equals(banci.trim()))
			params.put("fdMinesumNight",banCaikuang);
		else if("中早班".equals(banci.trim()))
			params.put("fdMineSumUpMid",banCaikuang);
		else if("中晚班".equals(banci.trim()))
			params.put("fdMineSumDownMid",banCaikuang);
		else{
		}
		dao.update("ZMCGDispatchDaily.update", params);
		return "";
	}
	
	/**
	 * <p>Function: generateGrub</p>
	 * <p>Description: </p>
	 * 生成调度日报中的掘进数据
	 * fdWorknumId:班次ID
	 * fdVirtualDate：调度日期
	 * fdMiningId：矿井ID
	 * fdMiningName：矿井名称
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-9-1 22:32:38 王伟
	 */
	@SuppressWarnings("unchecked")
	public static String generateGrub(String fdWorknumId,String fdVirtualDate,String fdMiningId,String fdMiningName){
		HashMap<Object, Object> params = new HashMap<Object, Object>();
		
		params = getDispatchDaily(fdVirtualDate,fdMiningId);//矿区调度日志主表的id
		String banci = getWorkScheduleName(fdWorknumId);//班次名称
		BigDecimal banJuejin = getGrubSumForDay(fdWorknumId,fdVirtualDate,fdMiningId,1); //班掘进量
		BigDecimal riJuejin = getGrubSumForDay("",fdVirtualDate,fdMiningId,2); //日掘进量
		BigDecimal yueJuejin = GrubSumForMonthYear(fdVirtualDate,fdMiningId,1); //月掘进量
		BigDecimal nianJuejin = GrubSumForMonthYear(fdVirtualDate,fdMiningId,2); //年掘进量

        //更新矿区调度日志表的数据
		params.put("fdKeyId",params.get("fdKeyId"));
		params.put("fdVirtualDate", fdVirtualDate);
		params.put("fdMiningId", fdMiningId);
		params.put("fdMiningName",fdMiningName);
		params.put("fdMovelenghDsum",riJuejin);
		params.put("fdMovelenghMsum",yueJuejin);
		params.put("fdMovelenghYsum",nianJuejin);
		if("早班".equals(banci.trim()))
			params.put("fdMovelenghDay",banJuejin);
		else if("中班".equals(banci.trim()))
			params.put("fdMovelenghMiddle",banJuejin);
		else if("晚班".equals(banci.trim()))
			params.put("fdMovelenghNight",banJuejin);
		else if("中早班".equals(banci.trim()))
			params.put("fdMoveLenghUpMid",banJuejin);
		else if("中晚班".equals(banci.trim()))
			params.put("fdMoveLenghDownMid",banJuejin);
		else{
		}
		dao.update("ZMCGDispatchDaily.update", params);
		return "";
	}
	/**
	 * 
	 * @param fdVirtualDate 调度日期
	 * @param fdMiningId  矿井id
	 * @param type 1表示月累计；2表示年累计。
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	private static BigDecimal GrubSumForMonthYear(String fdVirtualDate, String fdMiningId, int type) {
		HashMap<Object, Object> params = new HashMap<Object, Object>();
		BigDecimal grubSum = null;
		String year = StaticTools.getYearStr(fdVirtualDate);
		String month = StaticTools.getMonthStr(fdVirtualDate); 
        if(type == 1){
        	params.clear();
        	params.put("fdYear", year);
        	params.put("fdMonth", month);
        	params.put("fdMiningId", fdMiningId);
    		List list = dao.query("ZMCGGrubReportFinal.querySUM",params);
    		if(list!=null&&list.size()>0){
    			ZMCGGrubReportFinal _tmpbean = (ZMCGGrubReportFinal)list.get(0);
    			grubSum = _tmpbean.getFdMoveLength();
    		}
        }else{
        	params.clear();
        	params.put("fdYear", year);
        	params.put("fdEndMonth", month);
        	params.put("fdMiningId", fdMiningId);
    		List list = dao.query("ZMCGGrubReportFinal.querySUM",params);
    		if(list!=null&&list.size()>0){
    			ZMCGGrubReportFinal _tmpbean = (ZMCGGrubReportFinal)list.get(0);
    			grubSum = _tmpbean.getFdMoveLength();
    		}
        }
        	
		return grubSum;
	}
	/**
	 * 
	 * @param fdWorknumId  班次id
	 * @param fdVirtualDate 调度日期
	 * @param fdMiningId  矿井id
	 * @param type 1表示按班次统计，2表示按日期统计
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	private static BigDecimal getGrubSumForDay(String fdWorknumId,String fdVirtualDate, String fdMiningId,int type) {
		HashMap<Object, Object> params = new HashMap<Object, Object>();
		if(1==type)
			params.put("fdWorknumId", fdWorknumId);
		params.put("fdVirtualDate", fdVirtualDate);
		params.put("fdMiningId", fdMiningId);
		List list = dao.query("ZMCGGrubReportFinal.querySUM",params);
		BigDecimal ban = null; //班掘进量
        if(list!=null&&list.size()>0){
        	ZMCGGrubReportFinal _tmpbean = (ZMCGGrubReportFinal)list.get(0);
        	ban = _tmpbean.getFdMoveLength();
        }              	
        return ban;
	}
	
	@SuppressWarnings("rawtypes")
	public static void copy(String sql, Map map) {
		dao.insert(sql, map);
	}
	
	@SuppressWarnings("rawtypes")
	public static boolean validate(String sql, Map map) {
		List result = dao.query(sql, map);
		if(result != null && result.size() > 0) {
			return true;
		}
		return false;
	}
}

